package com.example.controller;


import com.example.config.MqttConfigurationProperties;
import com.example.service.MqttMessageSender;
import com.example.service.MqttMessageSubscriber;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author ysb
 * @version V1.0
 * @Date 2024/11/12
 **/
@RestController
@RequestMapping(value = "/api/test")
public class TestMqttToDeviceController {

    @Resource
    private MqttMessageSender mqttMessageSender;
    @Resource
    private MqttMessageSubscriber mqttMessageSubscriber;
    @Resource
    private MqttConfigurationProperties mqttConfigurationProperties;

    /**
     * 发送消息
     */
    @GetMapping(value = "/hello")
    public String sayHello() {
        String DEFAULT_TOPIC = mqttConfigurationProperties.getSubTopic().split(",")[0]; // topic/test
        String msg = "hello mqtt-springboot";

        mqttMessageSender.sendMessage(DEFAULT_TOPIC, msg);
        return "发送消息".concat(msg);
    }

    /**
     * 发送消息
     */
    @GetMapping(value = "/send-msg")
    public String sendMsg(@RequestParam String topic, @RequestParam String msg) {
        mqttMessageSender.sendMessage(topic, msg);
        return "发送消息".concat(msg);
    }

    /**
     * 发送消息
     * @param qos 0,1,2
     */
    @GetMapping(value = "/send-msg/qos")
    public String sendMsg(@RequestParam String topic, @RequestParam Integer qos, @RequestParam String msg) {
        mqttMessageSender.sendMessage(topic, qos, msg);
        return "发送消息".concat(msg);
    }

    /**
     * 添加订阅
     */
    @GetMapping(value = "/subscribe")
    public String subscribeTopic(@RequestParam String topic) {
        return mqttMessageSubscriber.subscribeTopic(topic);
    }
}
